<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // o(n*2)
      function insertSort(arr) {
        let temp;
        for (let i = 0; i < arr.length; i++) {
          temp = arr[i];
          for (let j = i; j >= 0; j--) {
            if (arr[j - 1] > temp) {
              // 不在该位置，将数组元素依次往后面移
              arr[j] = arr[j - 1];
            } else {
              arr[j] = temp;
              break;
            }
          }
        }
        return arr;
      }
      console.log(insertSort([1, 4, 5, 7, 8, 9, 6, 100, 99]));
    </script>
  </body>
</html>
